var __spreadArrays = (this && this.__spreadArrays) || function () {
    for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
    for (var r = Array(s), k = 0, i = 0; i < il; i++)
        for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
            r[k] = a[j];
    return r;
};
var Store = /** @class */ (function () {
    function Store(reducer, initMessage) {
        this.reducer = reducer;
        this._listeners = new Array();
        this._messages = initMessage;
    }
    Store.prototype.getState = function () {
        return this._messages;
    };
    ;
    Store.prototype.subscribe = function (listener) {
        var _this = this;
        this._listeners.push(listener);
        return function () {
            _this._listeners = _this._listeners
                .filter(function (currentListener) { return listener !== currentListener; });
        };
    };
    Store.prototype.dispatch = function (action) {
        this._messages = this.reducer(this._messages, action);
        this._listeners.forEach(function (listener) {
            listener();
        });
    };
    return Store;
}());
var DELETE_MESSAGE = 'DELETE_MESSAGE';
var ADD_MESSAGE = 'ADD_MESSAGE';
var reducer = function (state, action) {
    switch (action.type) {
        case DELETE_MESSAGE:
            return __spreadArrays(state.slice(0, action.index), state.slice(action.index + 1, state.length - 1));
        case ADD_MESSAGE:
            return __spreadArrays(state, [action.message]);
        default:
            return state;
    }
};
var store = new Store(reducer, ['Listen! This is a dialog!']);
store.subscribe(function () {
    store.getState().forEach(function (message) {
        console.log(message + '\n');
    });
});
store.dispatch({ type: ADD_MESSAGE, message: 'Jerry: How are you?' });
store.dispatch({ type: ADD_MESSAGE, message: 'Tom: I\'m fine, and you?' });
store.dispatch({ type: ADD_MESSAGE, message: 'Jerry: I\'m fine too, thanks!' });
